package top.tagao.m01_structuralPatterns.s_01_proxyDesign.proxy_02_dynamic.dy_02_cglib;

import net.sf.cglib.proxy.Enhancer;
import top.tagao.m01_structuralPatterns.s_01_proxyDesign.proxy_01_static.ClothFactory;
import top.tagao.m01_structuralPatterns.s_01_proxyDesign.proxy_01_static.NikeClothFactory;

/**
 * @author fu-xiao-liu
 * @Date 2022/3/26 20:11
 */
public class CglibProxy {
    public static void main(String[] args) {
        Class clazz = SmsService.class;

        //1.创建动态代理增强类
        Enhancer enhancer = new Enhancer();
        //2.设置类加载器
        enhancer.setClassLoader(clazz.getClassLoader());
        //3.设置被代理类
        enhancer.setSuperclass(clazz);
        //4.设置方法拦截
        enhancer.setCallback(new DebugMethodInterceptor());

        SmsService smsService = (SmsService)enhancer.create();
        smsService.send("你好，中国");

        System.out.println("---------------");

         clazz = NikeClothFactory.class;
        //1.创建动态代理增强类
         enhancer = new Enhancer();
        //2.设置类加载器
        enhancer.setClassLoader(clazz.getClassLoader());
        //3.设置被代理类
        enhancer.setSuperclass(clazz);
        //4.设置方法拦截
        enhancer.setCallback(new DebugMethodInterceptor());

        ClothFactory clothFactory = (ClothFactory)enhancer.create();
        clothFactory.produceCloth();
    }
}
